"""
@name : IP地址
@author : liuyi
@projectname: Demo01
@time : 2022/3/5  18:12
"""

import random

#生成IP文件和统计次数
ip_dict = {}
with open("ip.txt", "w+", encoding="utf-8") as fp, \
        open("ip.txt", "r", encoding="utf-8") as fp1:
    for i in range(1200000):
        num = random.randint(0, 255)
        ip = "172.25.254."
        ip_new = ip + str(num) + "\n"
        fp.write(ip_new)
    fp.flush()
    for i in fp1:
        i = i.replace("\n", "")
        if i in ip_dict.keys():
            ip_dict[i] = ip_dict[i] + 1
        else:
            ip_dict[i] = 1

#统计次数出现前10的IP地址
conter = 0
ip_dict_new = {}
while conter < 10:
    ip_key = "";ip_values = 0;num_conter = 0
    for i, j in ip_dict.items():
        if num_conter < j:
            num_conter = j
            ip_key = i
            ip_values = j
    ip_dict_new[ip_key] = ip_values
    ip_dict.pop(ip_key, "")
    conter += 1

#格式化输出
num = 1
for i, j in ip_dict_new.items():
    print(f"出现次数第{num}的IP地址为：{i}\t\t次数为{j}")
    num += 1
